﻿using SqlSugar;
using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    public class UserModel
    {
        public int id { get; set; }
        public string account { get; set; }
    }

    public class ContractTool
    {
        public bool Init()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=121.196.23.49;Database=crm_yddsj_net;Uid=root;Pwd=bca179e83f23af41;SslMode=none;AllowLoadLocalInfile=true;",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });

            try
            {
                var isSuccess = true;
                var sqlStr = "select id, account from oa_crm_user where account in (SELECT create_by FROM `oa_crm_contract` where sales_id is null)";
                var list = db.SqlQueryable<UserModel>(sqlStr).ToList();
                foreach (var record in list)
                {
                    var sqlUpdateStr = $"update `oa_crm_contract` set `sales_id` = {record.id} WHERE `create_by`='{record.account}'";


                    var result = db.Ado.ExecuteCommand(sqlUpdateStr);
                    Console.WriteLine(result);
                    if(result <= 0) isSuccess = false;
                }

                return isSuccess;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}